package com.dcrzl.shcz.module.sys.controller;

import cn.hutool.core.io.FileUtil;
import com.dcrzl.shcz.common.entity.sys.SysFile;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.module.sys.service.SysFileService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;



/**
 * 
 *
 * @author dcrzl
 * @email 1728795786@qq.com
 * @date 2024-08-28 11:55:09
 */
@RestController
@RequestMapping("/sys/file")
public class SysFileController {
    @Autowired
    private SysFileService sysFileService;

    @Value("${my.file.path}")
    private String baseFilePath;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result<List<SysFile>> list(SysFile sysFile){
       return sysFileService.selectList(sysFile);
    }

    /**
    * 列表分页
    */
    @GetMapping("/page")
    public PageInfo<SysFile> page(SysFile sysFile){
        return sysFileService.selectPage(sysFile);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public Result<SysFile> info(@PathVariable("id") Long id){
		return sysFileService.selectById(id);
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody SysFile sysFile){
		return sysFileService.insert(sysFile);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result<Integer> update(@RequestBody SysFile sysFile){
		return sysFileService.updateById(sysFile);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result<Integer> delete(@RequestBody Long[] ids){
        return sysFileService.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 文件上传
     * @param files
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public Result<SysFile> uploadFile(@RequestParam("file") MultipartFile[] files) {
        return sysFileService.uploadFile(Arrays.asList(files), null);
    }

    /**
     * 文件回显or下载接口
     *
     * @param path
     * @param response
     * @throws IOException
     */
    @GetMapping("/path/{path}/{name}")
    public void download(@PathVariable String path,@PathVariable String name, HttpServletResponse response) throws IOException {
        // 根据文件的唯一标识码获取文件
        File uploadFile = new File(baseFilePath + "/"+path+"/"+name);
        // 设置输出流的格式
        ServletOutputStream os = response.getOutputStream();
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));

//        if(name.contains(".mp4")){
//            response.setContentType("application/octet-stream");
//        }else{
//            response.setContentType("application/octet-stream");
//        }
        // 读取文件的字节流
        os.write(FileUtil.readBytes(uploadFile));
        os.flush();
        os.close();
    }

}
